//
//  AuthenMccListVC.swift
//  kxf
//
//  Created by Mac on 2024/3/7.
// URL_Mcc_type

import Foundation
import MJRefresh
import MCToast

class AuthenMccListVC: WHBaseViewVC {
    
    var mccType = "mcc"
    var merchantType = "1" // 0  企业   1 小微  2 个体
    
    var dataSourceArr = NSMutableArray()
    var pageNum = 1
    let pageSize = 20
    var keywords = ""
    
    var posChannel = ""
    
    var tapBlock:((NSDictionary)->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendMccListRequest()
    }
    
    lazy var searchVm : SearchVM = {
        let vm = SearchVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight(), width: 0, height: 0))
        vm.searchBlock = {()in
            self.keywords = self.searchVm.textField.text ?? ""
            self.pageNum = 1
            self.sendMccListRequest()
        }
        return vm
    }()
    lazy var tableView : UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: self.searchVm.frame.maxY+kFitWidth(5), width: SCREEN_WIDHT, height: SCREEN_HEIGHT - (self.searchVm.frame.maxY+kFitWidth(5))), style: .plain)
        vi.delegate = self
        vi.dataSource = self
        vi.separatorStyle = .none
        vi.backgroundColor = .white
        vi.register(MCCTableViewCell.classForCoder(), forCellReuseIdentifier: "MCCTableViewCell")
        
        vi.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendMccListRequest()
        })

        vi.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendMccListRequest()
        })
        
        return vi
    }()
    lazy var emptyView : EmptyViewWithBtn = {
        let emptyViewT = EmptyViewWithBtn.init(frame: CGRect.zero)
        emptyViewT.isHidden = true
        emptyViewT.contentLabel.text = "没有查询到数据~"
        
        return emptyViewT
    }()
}

extension AuthenMccListVC{
    func initUI() {
        if self.mccType == "mcc"{
            initNavi(titleStr: "所属行业")
        }else if self.mccType == "alipay"{
            initNavi(titleStr: "支付宝分类")
        }else if self.mccType == "wechat"{
            initNavi(titleStr: "微信分类")
        }
        
        view.addSubview(searchVm)
        view.addSubview(tableView)
        
        tableView.addSubview(emptyView)
        emptyView.center = CGPoint.init(x: SCREEN_WIDHT*0.5, y: (SCREEN_HEIGHT - self.searchVm.frame.maxY)*0.5)
    }
}

extension AuthenMccListVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        emptyView.isHidden = dataSourceArr.count > 0 ? true : false
        return dataSourceArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MCCTableViewCell") as! MCCTableViewCell
        let dict = self.dataSourceArr[indexPath.row]as? NSDictionary ?? [:]
        
        if self.mccType == "mcc"{
            cell.updateUIForMcc(dict: dict)
        }else if self.mccType == "wechat" || self.mccType == "alipay"{
            cell.updateUIForWeChat(dict: dict)
        }
        
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kFitWidth(44)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.tapBlock != nil{
            let dict = self.dataSourceArr[indexPath.row]as? NSDictionary ?? [:]
            self.tapBlock!(dict)
        }
        self.backTapAction()
    }
}
extension AuthenMccListVC{
    func sendMccListRequest() {
        var urlString = URL_Mcc_type
        
        var paramString = ""
        let commomString = "&pageNum=\(pageNum)&pageSize=\(pageSize)&merchantNo=T201908211005&channel=\(posChannel)"
        if self.mccType == "mcc"{
            paramString = "?businessName=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&mchType=1\(commomString)"
        }else if self.mccType == "wechat"{
            urlString = URL_Mcc_type_alipay
            
            if self.merchantType == "0"{
                let merchantTypeString = "企业"
                paramString = "?classTypeThree=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&type=1&merchantType=\(merchantTypeString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")\(commomString)"
            }else if self.merchantType == "1"{
                let merchantTypeString = "小微"
                paramString = "?classTypeThree=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&mchType=1&type=1&merchantType=\(merchantTypeString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")\(commomString)"
            }else{
                let merchantTypeString = "个体工商户"
                paramString = "?classTypeThree=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&type=1&merchantType=\(merchantTypeString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")\(commomString)"
            }
            
            //mchType=1
        }else if self.mccType == "alipay"{
            urlString = URL_Mcc_type_alipay
            let mchTypeString = self.merchantType == "1" ? "&mchType=1" : ""
            paramString = "?classTypeThree=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&mchType=1&type=0\(commomString)\(mchTypeString)"
        }else if self.mccType == "areaCode"{
            urlString = URL_Mcc_Area_Code
            paramString = "?classTypeThree=\(keywords.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&"
        }
        
        WHNetworkUtil.shareManager().GET(urlString: "\(urlString)\(paramString)", vc: self) { responseObject in
            DLLog(message: responseObject)
            self.tableView.mj_footer!.endRefreshing()
            self.tableView.mj_header!.endRefreshing()
            
            if self.pageNum == 1{
                self.dataSourceArr.removeAllObjects()
            }
            
            let data = responseObject["data"]as? NSDictionary ?? [:]
            let dataArr = data["list"]as? NSArray ?? []
            self.dataSourceArr.addObjects(from: dataArr as! [Any])
            // 提示没有更多数据
            if dataArr.count < self.pageSize {
                self.tableView.mj_footer!.endRefreshingWithNoMoreData()
            }
            
            self.tableView.reloadData()
        }
    }
}
